package com.daocao.yfb.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import com.daocao.yfb.R;
import com.daocao.yfb.api.model.RelateListModel;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class ReceiveAccountControlAdapter extends ItemAdapter<RelateListModel.DatasBean, ReceiveAccountControlAdapter.DefaultHolder> {

    private Context context;
    private boolean showCheck = false;

    public ReceiveAccountControlAdapter(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_receive_account, parent));
    }

    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        RelateListModel.DatasBean item = getItem(position);
        holder.txtAccount.setText(item.account);
        if (showCheck) {
            holder.cbCheck.setVisibility(View.VISIBLE);
        } else {
            holder.cbCheck.setVisibility(View.GONE);
        }
        if (item.isCheck) {
            holder.cbCheck.setImageResource(R.mipmap.img_cb_check);
        } else {
            holder.cbCheck.setImageResource(R.mipmap.img_cb_uncheck);
        }


        holder.layoutCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onChooseListener != null) {
                    onChooseListener.onClick(item.id);
                    if (getmSource().get(position).isCheck) {
                        getmSource().get(position).isCheck = false;
                    } else {
                        getmSource().get(position).isCheck = true;
                    }
                    notifyDataSetChanged();
                }
            }
        });


    }

    public void setShowCheck(boolean check) {
        this.showCheck = check;
        notifyDataSetChanged();
    }

    class DefaultHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.txt_account)
        TextView txtAccount;
        @BindView(R.id.cb_check)
        ImageView cbCheck;
        @BindView(R.id.layout_check)
        LinearLayout layoutCheck;

        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    private OnChooseListener onChooseListener;

    public void setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
    }

    public interface OnChooseListener {
        void onClick(String id);
    }
}
